package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.domain.PmsBrand;
import com.sky.domain.PmsCategory;
import com.sky.domain.PmsCategoryBrandRelation;
import com.sky.exception.BizException;
import com.sky.mapper.PmsBrandMapper;
import com.sky.mapper.PmsCategoryMapper;
import com.sky.service.PmsCategoryBrandRelationService;
import com.sky.mapper.PmsCategoryBrandRelationMapper;
import com.sky.utils.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Administrator
* @description 针对表【pms_category_brand_relation(品牌分类关联)】的数据库操作Service实现
* @createDate 2024-06-30 16:46:42
*/
@Service
public class PmsCategoryBrandRelationServiceImpl extends ServiceImpl<PmsCategoryBrandRelationMapper, PmsCategoryBrandRelation> implements PmsCategoryBrandRelationService{

    @Resource
    private PmsCategoryMapper pmsCategoryMapper;
    @Resource
    private PmsBrandMapper pmsBrandMapper;

    @Override
    public List<PmsCategoryBrandRelation> listByBrandId(Long brandId) {
        List<PmsCategoryBrandRelation> relationList = baseMapper.selectList(new LambdaQueryWrapper<PmsCategoryBrandRelation>().eq(PmsCategoryBrandRelation::getBrandId, brandId));
        return relationList;
    }

    @Override
    public boolean addRelation(PmsCategoryBrandRelation pmsCategoryBrandRelation) {
        PmsCategoryBrandRelation brandRelation = baseMapper.selectOne(new LambdaQueryWrapper<PmsCategoryBrandRelation>()
                .eq(PmsCategoryBrandRelation::getBrandId, pmsCategoryBrandRelation.getBrandId())
                .eq(PmsCategoryBrandRelation::getCatalogId, pmsCategoryBrandRelation.getCatalogId()));
        if (StringUtils.isNotEmpty(brandRelation)){
            throw new BizException("该品牌已关联该分类");
        }

        PmsCategory pmsCategory = pmsCategoryMapper.selectById(pmsCategoryBrandRelation.getCatalogId());
        pmsCategoryBrandRelation.setCatalogName(pmsCategory.getName());
        PmsBrand pmsBrand = pmsBrandMapper.selectById(pmsCategoryBrandRelation.getBrandId());
        pmsCategoryBrandRelation.setBrandName(pmsBrand.getName());
        baseMapper.insert(pmsCategoryBrandRelation);

        return true;
    }

    @Override
    public boolean updateRelation(PmsCategoryBrandRelation pmsCategoryBrandRelation) {
        PmsCategoryBrandRelation brandRelation = baseMapper.selectOne(new LambdaQueryWrapper<PmsCategoryBrandRelation>()
                .eq(PmsCategoryBrandRelation::getBrandId, pmsCategoryBrandRelation.getBrandId())
                .eq(PmsCategoryBrandRelation::getCatalogId, pmsCategoryBrandRelation.getCatalogId()));
        if (StringUtils.isNotEmpty(brandRelation)){
            throw new BizException("该品牌已关联该分类");
        }

        PmsCategory pmsCategory = pmsCategoryMapper.selectById(pmsCategoryBrandRelation.getCatalogId());
        pmsCategoryBrandRelation.setCatalogName(pmsCategory.getName());
        PmsBrand pmsBrand = pmsBrandMapper.selectById(pmsCategoryBrandRelation.getBrandId());
        pmsCategoryBrandRelation.setBrandName(pmsBrand.getName());
        baseMapper.updateById(pmsCategoryBrandRelation);

        return true;
    }

    @Override
    public List<PmsCategoryBrandRelation> listByCatId(Long catId) {
        List<PmsCategoryBrandRelation> relationList = baseMapper.selectList(new LambdaQueryWrapper<PmsCategoryBrandRelation>().eq(PmsCategoryBrandRelation::getCatalogId, catId));
        return relationList;
    }
}




